<?php
/**
 * Created by PhpStorm.
 * User: lingyun
 * Date: 2018/3/19
 * Time: 20:50
 * Desc: 网站设置服务层
 */
namespace app\admin\service;
use think\Controller;
use think\Db;

class WebsiteService extends Controller{
    /**
     * @Author  lingyun
     * @Desc    会员等级设置
     * @param $param 更新的数据
     */
    public function add_edit_level($param){
        if(empty($param['title'])){
            return ['code'=>0,'msg'=>'请输入等级名称'];
        }else if($param['score'] === ''){
            return ['code'=>0,'msg'=>'请输入升级金额'];
        }else if(empty($param['detail'])){
            return ['code'=>0,'msg'=>'请输入等级简介'];
        }
        if(isset($param['id'])){     //编辑会员等级
            $result = db('sys_level')->where('id',$param['id'])->update($param);
            if(false === $result){
                return ['code' => 0, 'data' => '', 'msg' => '保存失败'];
            }else{
                return ['code' => 1, 'data' => '', 'msg' => '编辑成功'];
            }
        }else{
            $result = db('sys_level')->insert($param);
            db('sys_level')->where('id',$result)->update(['code'=>$result]);
            if(false === $result){
                return ['code' => 0, 'data' => '', 'msg' => $this->getError()];
            }else{
                return ['code' => 1, 'data' => '', 'msg' => '添加成功'];
            }
        }
    }

    /**
     * @Author  lingyun
     * @Desc    获取会员等级信息
     * @param $id   会员等级规则
     */
    public function get_member_level($id){
        $arr = db('sys_level')->where('id',$id)->find();

        $this->assign('id',$id);
        $this->assign('info',$arr);
    }

    /**
     * @Author  lingyun
     * @Desc    删除会员等级记录
     * @param $id   记录id
     */
    public function del_member_level($id){
        $res = db('user')->where('id',$id)->delete();
        if($res>0){
            return ['code'=>1,'msg'=>'删除成功'];
        }else{
            return ['code'=>0,'msg'=>'删除失败'];
        }
    }
}
